import apiSV from './apiSV'
import * as express from 'express'
import * as moment from "moment"

/** 问卷信息/列表/对象 获取 */
class teacherlist {

  /** 获取调研问卷列表 */
  async getSurveyList(req: express.Request) {
    try {
      let data = await apiSV(req.api.v4.get, {
        action: 'corpsurvey.survey.m.list',
        sessionKey: req.t,
        companyID: req.companyId,
        pageSize: req.query.pageSize || 10,
        pageIndex: req.query.page || 1,
        keyword: req.query.keyword || '',
        complete: req.query.complete || '',
        idList: req.query.surveyId || ''
      })
      return data.data.ml
    } catch (e) {
      return Promise.reject(e)
    }
  }

  /** 获取调研问卷obj对象 */
  async getObj(req: express.Request) {
    if (req.query.noObj) return Promise.resolve(null)
    try {
      let data = await apiSV(req.api.v4.get, {
        action: 'corpsurvey.surveyobj.m.list',
        sessionKey: req.t,
        companyID: req.companyId,
        pageSize: req.query.pageSize || 5,
        pageIndex: req.query.page || 1,
        isPaging: req.query.isPaging === undefined ? 'true' : req.query.isPaging,
        surveyID: req.query.surveyId
      })
      return data.data.ml
    } catch (e) {
      return Promise.reject(e)
    }
  }

  /** 获取调研问卷题目 */
  async getQuestionList(req: express.Request) {
    try {
      let data = await apiSV(req.api.v4.get, {
        action: 'corpsurvey.surveyquestion.list',
        sessionKey: req.t,
        companyID: req.companyId,
        pageSize: req.query.pageSize || 10,
        pageIndex: req.query.pageIndex || 1,
        isPaging: req.query.isPaging === undefined ? 'false' : req.query.isPaging,
        surveyID: req.query.surveyId,
      })
      return data.data.ml
    } catch (e) {
      return Promise.reject(e)
    }
  }

  async getAdminSurveyItem(req: express.Request) {
    try {
      let data = await apiSV(req.api.v4.get, {
        action: 'corpsurvey.survey.m.get',
        sessionKey: req.t,
        companyID: req.companyId,
        surveyID: req.query.surveyId
      })
      return data.data.md
    } catch (e) {
      return Promise.reject(e)
    }
  }

  /** 个人版--获取单个问卷 */
  async getPersonalSurvey(req: express.Request) {
    try {
      let data = await apiSV(req.api.v4.get, {
        action: 'corpsurvey.survey.get',
        sessionKey: req.t,
        companyID: req.companyId,
        surveyID: req.query.surveyId
      })
      return data.data.md
    } catch (e) {
      return Promise.reject(e)
    }
  }

  /** 个人版--获取单个问卷含题目 */
  async getPersonalSurveyInfo(req: express.Request) {
    try {
      let survey = await this.getPersonalSurvey(req)
      let questions = null
      /** 开始之后才可获取问卷 */
      if (moment().isAfter(survey.startTime)) {
        let data = await apiSV(req.api.v4.get, {
          action: 'corpsurvey.surveyquestion.list',
          sessionKey: req.t,
          companyID: req.companyId,
          surveyID: req.query.surveyId,
          ispaging: 'false'
        })
        questions = data.data.ml
      }
      return { survey, questions }
    } catch (e) {
      return Promise.reject(e)
    }
  }



  /** 获取单个问卷所有信息 必须有req.query.surveyId */
  async getSurveyInfo(req: express.Request) {
    try {
      req.query.isPaging = 'false'
      let [survey, obj, questions] = await Promise.all([
        this.getAdminSurveyItem(req),
        this.getObj(req),
        this.getQuestionList(req)
      ])
      return { survey, obj, questions }
    } catch (e) {
      return Promise.reject(e)
    }
  }

  /** 获取题库题目列表 */
  async getQuestionBankList(req: express.Request) {
    let data = await apiSV(req.api.v4.get, {
      action: 'corpsurvey.surveyquestiondatabase.m.list',
      sessionKey: req.t,
      companyID: req.companyId,
      pageIndex: req.query.page || 1,
      pageSize: req.query.pageSize || 20,
      keyword: req.query.keyword || "",
      questionType: req.query.questionType || "",
      dbid: req.query.bankType == 0 ? 0 : req.companyId,
      idList: req.query.idList || "",
      isPaging: req.query.isPaging || "true"
    })
    return data.data.ml
  }
}

const teacherlistSV = new teacherlist()
export default teacherlistSV